<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息管理</title>
    <style>
        label,
        input {
            display: block;
            /* 将标签转换为块级元素，使其垂直排列 */
            margin-top: 10px;
            /* 添加一些顶部间距 */
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th,
        td {
            padding: 8px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f2f2f2;
        }

        caption {
            font-size: 1.5em;
        }

        thead td {
            font-size: 1.2em;
        }
    </style>
</head>

<body>
    <!-- 学生信息输入表单 -->
    <label>姓名：</label><input type="text" placeholder="请输入学生姓名" id="name">
    <label>性别：</label>
    <select id="gender">
        <option value="男">男</option>
        <option value="女">女</option>
    </select>
    <label>年龄：</label><input type="text" placeholder="请输入学生年龄" id="age">
    <input type="button" value="提交" onclick="setInfo()">

    <!-- 表格 -->
    <table>
        <thead>
            <caption>22级软件1班信息表</caption>
            <tr>
                <td>姓名</td>
                <td>性别</td>
                <td>年龄</td>
            </tr>
        </thead>
        <tbody id="students">
            <!-- 这里将会动态添加内容 -->
        </tbody>
    </table>

    <script>
        initStudents();

        function initStudents() {
            const xhr = new XMLHttpRequest()
            let method = 'get'
            let url = '/getstu'
            xhr.open(method, url, true)
            xhr.send(null)
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let res = xhr.responseText;
                    let stus = JSON.parse(res)
                    showStudents(stus)
                    console.log(typeof (stus))
                }
            }
        }

        function showStudents(students) {
            let tbody = document.getElementById('students');
            tbody.innerHTML = '';
            for (let i = 0; i < students.length; i++) {
                let stu = students[i];
                tbody.innerHTML = tbody.innerHTML + '<tr><td>' + stu.name + '</td><td>' + stu.gender + '</td><td>' + stu.age + '</td></tr>';
            }
        }

        function setInfo() {
            let name = document.getElementById('name').value;
            let gender = document.getElementById('gender').value;
            let age = document.getElementById('age').value;
            submit({ name: name, gender: gender, age: age })//ajax 提交到后台
        }

        function submit(stu) {
            const xhr = new XMLHttpRequest()
            let method = 'post'
            let url = '/addstu'
            xhr.open(method, url, true)
            xhr.send(JSON.stringify(stu))
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let res = xhr.responseText;
                    let stus = JSON.parse(res)
                    showStudents(stus)
                    console.log(stus)
                }
            }
        }
    </script>

</body>

</html>